﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BBPA.BLL
{
    public partial class Goods
    {
        #region 满赠
        /// <summary>
        /// 获得参加供应商满赠活动赠送的数量
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static int GetGiftNumberBuy(object GoodsID_, object Number_)
        {
            int _Number = DataTypePlus.requestInt(Number_);
            //
            if (HaveGiftNumberBuy_ing(GoodsID_))
            {
                foreach (Model.Goods_SaleWay_GiftNumber modelGiftNumber in GetListGiftNumberBuy_ing(GoodsID_))
                {
                    if (_Number >= modelGiftNumber.Number) return modelGiftNumber.GiftNumberBuy;
                }
            }
            return 0; 
        }
        /// <summary>
        /// 获得参加供货商满赠活动之后实得的数量
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static int GetNumberBuy(object GoodsID_, object Number_)
        {
            return DataTypePlus.requestInt(Number_) + GetGiftNumberBuy(GoodsID_, Number_);
        }
        /// <summary>
        /// 获得参加联盟满赠活动赠送的数量
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static int GetGiftNumberSell(object GoodsID_, object Number_)
        {
            int _Number = DataTypePlus.requestInt(Number_);
            //
            if (HaveGiftNumberSell_ing(GoodsID_))
            {
                foreach (Model.Goods_SaleWay_GiftNumber modelGiftNumber in GetListGiftNumberSell_ing(GoodsID_))
                {
                    if (_Number >= modelGiftNumber.Number) return modelGiftNumber.GiftNumberSell;
                }
            }
            return 0;
        }
        /// <summary>
        /// 获得参加联盟满赠活动之后实得的数量
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static int GetNumberSell(object GoodsID_, object Number_)
        {
            return DataTypePlus.requestInt(Number_) + GetGiftNumberSell(GoodsID_, Number_);
        }
        #endregion

        #region 折扣
        /// <summary>
        /// 获得能参加的供货商的折扣活动的折扣点数
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static int GetBuyDiscount(object GoodsID_, object Number_)
        {
            int _Number = DataTypePlus.requestInt(Number_);
            BLL.Goods bllGoods = new Goods();
            Model.Goods modelGoods = bllGoods.GetModel(DataTypePlus.requestInt(GoodsID_));
            if (HaveDiscountBuy_ing(GoodsID_))
            {
                foreach (Model.Goods_SaleWay_Discount modelDiscount in GetListDiscountBuy_ing(GoodsID_))
                {
                    if (_Number * modelGoods.BuyPrice >= modelDiscount.Money) return modelDiscount.DiscountBuy;
                }
            }
            return 100;
        }
        /// <summary>
        /// 获得参加供货商折扣活动之后的实际联盟进货单价
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static decimal GetBuyPrice(object GoodsID_, object Number_)
        {
            BLL.Goods bllGoods = new Goods();
            Model.Goods modelGoods = bllGoods.GetModel(DataTypePlus.requestInt(GoodsID_));
            //
            return modelGoods.BuyPrice *Convert.ToDecimal( GetBuyDiscount(GoodsID_, Number_)) / 100;
        }
        /// <summary>
        /// 获得参加供货商折扣活动之后的实际联盟进货小计
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static decimal GetBuyPriceSum(object GoodsID_, object Number_){return GetBuyPrice(GoodsID_, Number_) *DataTypePlus.requestDecimal( Number_);}



        /// <summary>
        /// 获得能参加的联盟的折扣活动的折扣点数
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static int GetSellDiscount(object GoodsID_, object Number_)
        {
            int _Number = DataTypePlus.requestInt(Number_);
            BLL.Goods bllGoods = new Goods();
            Model.Goods modelGoods = bllGoods.GetModel(DataTypePlus.requestInt(GoodsID_));
            if (HaveDiscountSell_ing(GoodsID_))
            {
                foreach (Model.Goods_SaleWay_Discount modelDiscount in GetListDiscountSell_ing(GoodsID_))
                {
                    if (_Number * modelGoods.SellPrice >= modelDiscount.Money) return modelDiscount.DiscountSell;
                }
            }
            return 100;
        }
        /// <summary>
        /// 获得参加联盟折扣活动之后的实际联盟销售单价
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static decimal GetSellPrice(object GoodsID_, object Number_)
        {
            BLL.Goods bllGoods = new Goods();
            Model.Goods modelGoods = bllGoods.GetModel(DataTypePlus.requestInt(GoodsID_));
            //
            return modelGoods.SellPrice *Convert.ToDecimal(  GetSellDiscount(GoodsID_, Number_)) / 100;
            
        }
        /// <summary>
        /// 获得参加联盟折扣活动之后的实际联盟销售小计
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <param name="Number_"></param>
        /// <returns></returns>
        public static decimal GetSellPriceSum(object GoodsID_, object Number_) { return GetSellPrice(GoodsID_, Number_) *DataTypePlus.requestDecimal( Number_); }
        #endregion

        #region 判断是否有活动
        /// <summary>
        /// 本商品是否有折扣活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static bool HaveDiscount(object GoodsID_) { return GetListDiscount(GoodsID_).Count > 0; }
        /// <summary>
        /// 本商品供货商是否有正在进行的折扣活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static bool HaveDiscountBuy_ing(object GoodsID_) { return GetListDiscountBuy_ing(GoodsID_).Count > 0; }
        /// <summary>
        /// 本商品联盟是否有正在进行的折扣活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static bool HaveDiscountSell_ing(object GoodsID_) { return GetListDiscountSell_ing(GoodsID_).Count > 0; }
        /// <summary>
        /// 本商品是否有满赠活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static bool HaveGiftNumber(object GoodsID_) { return GetListGiftNumber(GoodsID_).Count > 0; }
        /// <summary>
        /// 本商品供货商是否有正在进行的满赠活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static bool HaveGiftNumberBuy_ing(object GoodsID_) { return GetListGiftNumberBuy_ing(GoodsID_).Count > 0; }
        /// <summary>
        /// 本商品联盟是否有正在进行的满赠活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static bool HaveGiftNumberSell_ing(object GoodsID_) { return GetListGiftNumberSell_ing(GoodsID_).Count > 0; }
        #endregion




        #region 获取活动List
        /// <summary>
        /// 获取本商品供货商正在进行的折扣活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static List<Model.Goods_SaleWay_Discount> GetListDiscountBuy_ing(object GoodsID_)
        {
            Goods_SaleWay_Discount bllGoods_SaleWay_Discount = new Goods_SaleWay_Discount();
            return bllGoods_SaleWay_Discount.GetModelList("GoodsID=" + GoodsID_ + " and DiscountBuy<100 and DateFrom<=getdate() and DateTo>=getdate() order by Money");
        }
        /// <summary>
        /// 获取本商品联盟正在进行的折扣活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static List<Model.Goods_SaleWay_Discount> GetListDiscountSell_ing(object GoodsID_)
        {
            Goods_SaleWay_Discount bllGoods_SaleWay_Discount = new Goods_SaleWay_Discount();
            return bllGoods_SaleWay_Discount.GetModelList("GoodsID=" + GoodsID_ + " and DiscountSell<100 and DateFrom<=getdate() and DateTo>=getdate() order by Money");
        }
        /// <summary>
        /// 获取本商品的所有折扣活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static List<Model.Goods_SaleWay_Discount> GetListDiscount(object GoodsID_)
        {
            Goods_SaleWay_Discount bllGoods_SaleWay_Discount = new Goods_SaleWay_Discount();
            return bllGoods_SaleWay_Discount.GetModelList("GoodsID=" + GoodsID_ + " order by Money");
        }
        /// <summary>
        /// 获取本商品供货商正在进行的满赠活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static List<Model.Goods_SaleWay_GiftNumber> GetListGiftNumberBuy_ing(object GoodsID_)
        {
            Goods_SaleWay_GiftNumber bllGoods_SaleWay_GiftNumber = new Goods_SaleWay_GiftNumber();
            return bllGoods_SaleWay_GiftNumber.GetModelList("GoodsID=" + GoodsID_ + " and GiftNumberBuy>0 and  DateFrom<=getdate() and DateTo>=getdate() order by Number");
        }
        /// <summary>
        /// 获取本商品联盟正在进行的满赠活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static List<Model.Goods_SaleWay_GiftNumber> GetListGiftNumberSell_ing(object GoodsID_)
        {
            Goods_SaleWay_GiftNumber bllGoods_SaleWay_GiftNumber = new Goods_SaleWay_GiftNumber();
            return bllGoods_SaleWay_GiftNumber.GetModelList("GoodsID=" + GoodsID_ + " and GiftNumberSell>0 and  DateFrom<=getdate() and DateTo>=getdate() order by Number");
        }
        /// <summary>
        /// 获取本商品的所有满赠活动
        /// </summary>
        /// <param name="GoodsID_"></param>
        /// <returns></returns>
        public static List<Model.Goods_SaleWay_GiftNumber> GetListGiftNumber(object GoodsID_)
        {
            Goods_SaleWay_GiftNumber bllGoods_SaleWay_GiftNumber = new Goods_SaleWay_GiftNumber();
            return bllGoods_SaleWay_GiftNumber.GetModelList("GoodsID=" + GoodsID_ + " order by Number");
        }
        #endregion
    }
}
